package com.cubeofnine.sugar.service.element;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

public class Contact implements KvmSerializable {
	private String id;
	private String first_name;
	private String last_name;
	private String company_name;
	private String job_title;
	private List<Phone> phones = new Vector<Phone>();
	private List<Email> emails = new Vector<Email>();
	private List<Address> addresses = new Vector<Address>();

	public Contact() {
		super();
	}

	public Contact(String id, String first_name, String last_name,
			String company_name, String job_title, List<Phone> phones,
			List<Email> emails, List<Address> addresses) {
		super();
		this.id = id;
		this.first_name = first_name;
		this.last_name = last_name;
		this.company_name = company_name;
		this.job_title = job_title;
		this.phones = phones;
		this.emails = emails;
		this.addresses = addresses;
	}

	public Contact(SoapObject obj) {
		super();
		try {
			this.id = obj.getProperty("id").toString();
		} catch (Exception e) {
			this.id = null;
		}

		try {
			this.first_name = obj.getProperty("first_name").toString();
		} catch (Exception e) {
			this.first_name = null;
		}

		try {
			this.last_name = obj.getProperty("last_name").toString();
		} catch (Exception e) {
			this.last_name = null;
		}

		try {
			this.company_name = obj.getProperty("company_name").toString();
		} catch (Exception e) {
			this.company_name = null;
		}

		try {
			this.job_title = obj.getProperty("job_title").toString();
		} catch (Exception e) {
			this.job_title = null;
		}

		try {
			List<?> list = (Vector<?>) obj.getProperty("phones");
			if (list != null) {
				Iterator<?> iter = list.iterator();
				while (iter.hasNext()) {
					addPhone(new Phone((SoapObject) iter.next()));
				}
			}
		} catch (Exception e) {
			this.phones = null;
		}

		try {
			List<?> list = (Vector<?>) obj.getProperty("emails");
			if (list != null) {
				Iterator<?> iter = list.iterator();
				while (iter.hasNext()) {
					addEmail(new Email((SoapObject) iter.next()));
				}
			}
		} catch (Exception e) {
			this.emails = null;
		}

		try {
			List<?> list = (Vector<?>) obj.getProperty("addresses");
			if (list != null) {
				Iterator<?> iter = list.iterator();
				while (iter.hasNext()) {
					addAddress(new Address((SoapObject) iter.next()));
				}
			}
		} catch (Exception e) {
			this.addresses = null;
		}
	}

	public Object getProperty(int index) {
		Object object = null;

		switch (index) {
		case 0:
			object = this.id;
			break;
		case 1:
			object = this.first_name;
			break;
		case 2:
			object = this.last_name;
			break;
		case 3:
			object = this.company_name;
			break;
		case 4:
			object = this.job_title;
			break;
		case 5:
			object = this.phones;
			break;
		case 6:
			object = this.emails;
			break;
		case 7:
			object = this.addresses;
			break;
		}

		return object;
	}

	public int getPropertyCount() {
		return 8;// Count GloVariable
	}

	public void getPropertyInfo(int index, Hashtable arg1,
			PropertyInfo propertyInfo) {
		switch (index) {
		case 0:
			propertyInfo.name = "id";
			propertyInfo.type = PropertyInfo.STRING_CLASS;
			break;
		case 1:
			propertyInfo.name = "first_name";
			propertyInfo.type = PropertyInfo.STRING_CLASS;
			break;
		case 2:
			propertyInfo.name = "last_name";
			propertyInfo.type = PropertyInfo.STRING_CLASS;
			break;
		case 3:
			propertyInfo.name = "company_name";
			propertyInfo.type = PropertyInfo.STRING_CLASS;
			break;
		case 4:
			propertyInfo.name = "job_title";
			propertyInfo.type = PropertyInfo.STRING_CLASS;
			break;
		case 5:
			propertyInfo.name = "phones";
			propertyInfo.type = PropertyInfo.VECTOR_CLASS;
			break;
		case 6:
			propertyInfo.name = "emails";
			propertyInfo.type = PropertyInfo.VECTOR_CLASS;
			break;
		case 7:
			propertyInfo.name = "addresses";
			propertyInfo.type = PropertyInfo.VECTOR_CLASS;
			break;
		}
	}

	public void setProperty(int index, Object obj) {
		switch (index) {
		case 0:
			this.id = obj.toString();
			break;
		case 1:
			this.first_name = obj.toString();
			break;
		case 2:
			this.last_name = obj.toString();
			break;
		case 3:
			this.company_name = obj.toString();
			break;
		case 4:
			this.job_title = obj.toString();
			break;
		case 5:
			this.phones = (Vector<Phone>) obj;
			break;
		case 6:
			this.emails = (Vector<Email>) obj;
			break;
		case 7:
			this.addresses = (Vector<Address>) obj;
			break;
		}
	}

	public String getFirst_name() {
		return first_name;
	}

	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}

	public String getLast_name() {
		return last_name;
	}

	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}

	public String getCompany_name() {
		return company_name;
	}

	public void setCompany_name(String company_name) {
		this.company_name = company_name;
	}

	public String getJob_title() {
		return job_title;
	}

	public void setJob_title(String job_title) {
		this.job_title = job_title;
	}

	public List<Phone> getPhones() {
		return phones;
	}

	public void setPhones(List<Phone> phones) {
		this.phones = phones;
	}

	public void addPhone(Phone phone) {
		this.phones.add(phone);
	}

	public List<Email> getEmails() {
		return emails;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}

	public void addEmail(Email email) {
		this.emails.add(email);
	}

	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}

	public void addAddress(Address address) {
		this.addresses.add(address);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "Contact [id=" + id + ", first_name=" + first_name
				+ ", last_name=" + last_name + ", company_name=" + company_name
				+ ", job_title=" + job_title + ", phones=" + phones
				+ ", emails=" + emails + ", addresses=" + addresses + "]";
	}

}
